//
//  OilOrderRootZeroVC.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/8/28.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "OilOrderRootZeroVC.h"
#import "OilOrderModel.h"
#import "OilOrderCell.h"


@interface OilOrderRootZeroVC ()<UITableViewDelegate,UITableViewDataSource>
{
     MBProgressHUD *progressHUD;
     UIImageView *noDrawImage;
     NSUInteger refreshIndex;
}

@property(nonatomic,strong)UITableView *oilView;

@property(nonatomic,strong)NSMutableArray *oilOrderArray;

@end

@implementation OilOrderRootZeroVC

static NSString * const oilID =  @"oilZeroReuseIdentifierCell";


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    refreshIndex = 1;
    
    [self buildView];
    [self oilOrderListRequestData:refreshIndex];
}

- (void)buildView
{
     self.oilView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT-116 ) style:UITableViewStyleGrouped];
    [self.view addSubview:_oilView];
    _oilView.delegate = self;
    _oilView.dataSource = self;
    
    _oilView.backgroundColor = BACKGROUND_COLOR;
    _oilView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_oilView registerNib:[UINib nibWithNibName:@"OilOrderCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:oilID];
    
    [self oilOrderDataRefreshing];
}

//刷新
- (void)oilOrderDataRefreshing
{
    //上啦刷新
    _oilView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(oilOrderListRequestMoreData)];
    
    //下拉刷新
    MJChiBaoZiHeader *header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(oilOrderListRequesetDownData)];
    
    // 隐藏时间 和状态
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
//    
//    [header beginRefreshing];
//    _oilView.mj_header = header;

}


//上啦更多
- (void)oilOrderListRequestMoreData
{
    refreshIndex ++;
    [self oilOrderListRequestData:refreshIndex];
    [_oilView.mj_footer endRefreshing];
}

- (void)oilOrderListRequesetDownData
{
    refreshIndex =1;
    [self oilOrderListRequestData:refreshIndex];
    [_oilView.mj_header endRefreshing];
}

#pragma mark >>>>>>>>>获取商品列表
- (void)oilOrderListRequestData:(NSInteger)index
{
    
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
//    //避免下拉刷新重复数据
//    if (refreshIndex ==1) {
//        [self.oilOrderArray removeAllObjects];
//    }
    
    NSDictionary *dict = @{@"username":USERNAME,@"page":[NSString stringWithFormat:@"%zd",index]};
    NSLog(@"%@",dict);
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"/api/Order/olilCardRecharge" completeBlock:^(id object) {

        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            
            NSArray *tempArr = [object objectForKey:@"data"];
            for (NSDictionary *tempDict in tempArr) {
                
                OilOrderModel *oilOrderModel = [[OilOrderModel alloc] initWithDictionary:tempDict error:nil];
                if (oilOrderModel) {
                    [self.oilOrderArray addObject:oilOrderModel];
                }
                
            }
            
        }else{
            
            //订单没有
            if (refreshIndex == 1) {
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noOrder"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/2);
                    make.height.mas_equalTo(HEIGHT/4);
                }];
            }else{
                
                //没有更多数据
                [_oilView.mj_footer endRefreshingWithNoMoreData];
                
            }

        }
        [progressHUD hide:YES];
        [_oilView reloadData];
        
    }];
    
}

#pragma mark >>>>>>>>>>申请退款通知
- (void)oilapplicationForDrawbackButtonClick:(UIButton *)sender
{
    
    OilOrderModel *oilModel = self.oilOrderArray[sender.tag];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"coastPushToDrawbackVC" object:nil userInfo:@{@"oid":oilModel.oid,@"pay_amount":oilModel.pay_amount}];
}

- (void)oilapplicationForPayOrderButtonClick:(UIButton *)sender
{
    OilOrderModel *oilModel = self.oilOrderArray[sender.tag];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"coastPushToPay" object:nil userInfo:@{@"oid":oilModel.oid,@"money":oilModel.pay_amount}];
}


#pragma mark >>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.oilOrderArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     OilOrderCell*cell=[tableView dequeueReusableCellWithIdentifier:oilID];
    
    if (cell==nil) {
        cell=[[OilOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:oilID];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    OilOrderModel *oilOrderModel = self.oilOrderArray[indexPath.section];

    //区分图片
    if ([oilOrderModel.amount isEqualToString:@"100"]) {
        cell.bgImg.image = [UIImage imageNamed:@"bg_oil100.jpg"];
    }else{
        cell.bgImg.image = [UIImage imageNamed:@"bg_oil500.jpg"];
    }
    
    
    //订单状态
    cell.statusPayLabel.textColor = [UIColor redColor];
    if ([oilOrderModel.status isEqualToString:@"0"]) {
        cell.statusPayLabel.text = @"未付款";
    }else if ([oilOrderModel.status isEqualToString:@"1"]){
        cell.statusPayLabel.text = @"充值成功";
    }else if ([oilOrderModel.status isEqualToString:@"2"]){
        cell.statusPayLabel.text = @"充值中";
    }else if ([oilOrderModel.status isEqualToString:@"3"]){
        cell.statusPayLabel.text = @"充值失败";
    }else if ([oilOrderModel.status isEqualToString:@"4"]){
        cell.statusPayLabel.text = @"已退款(充值失败)";
    }else{
        cell.statusPayLabel.text = @"";
    }
    
    
    //充值状态
    cell.drawbackBtn.titleLabel.font = FONT(12);
    cell.drawbackBtn.backgroundColor = DEFAULT_COLOR;
    cell.drawbackBtn.layer.cornerRadius = 3;
    cell.drawbackBtn.layer.masksToBounds = YES;
    
    if ([oilOrderModel.status_text isEqualToString:@"未付款"]) {
        cell.drawbackBtn.hidden = false;
        [cell.drawbackBtn setTitle:@"付款" forState:UIControlStateNormal];
        [cell.drawbackBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cell.drawbackBtn addTarget:self action:@selector(oilapplicationForPayOrderButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.drawbackBtn.tag = indexPath.section;
        
    }else if ([oilOrderModel.status isEqualToString:@"3"]){
        cell.drawbackBtn.hidden = false;
        cell.drawbackBtn.backgroundColor = [UIColor whiteColor];
        [cell.drawbackBtn setTitle:@"申请退款" forState:UIControlStateNormal];
        [cell.drawbackBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cell.drawbackBtn.layer.borderWidth = 0.5;
        cell.drawbackBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [cell.drawbackBtn addTarget:self action:@selector(oilapplicationForDrawbackButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.drawbackBtn.tag = indexPath.section;
        
    }else{
        
        cell.drawbackBtn.hidden = YES;
    }
    
    //订单号
    cell.oidOrderLabel.text = [NSString stringWithFormat:@"订单号: %@",oilOrderModel.oid];
    //油卡号
    cell.accountNoLabel.text = [NSString stringWithFormat:@"油卡号: %@",oilOrderModel.account];
    //姓名
    cell.nameLabel.text = [NSString stringWithFormat:@"姓名: %@",oilOrderModel.real_name];
    //电话
    cell.phoneN.text = [NSString stringWithFormat:@"电话: %@",oilOrderModel.phone];
    //充值金额
    cell.rcMoneyLabel.text = [NSString stringWithFormat:@"付款金额: %@元",oilOrderModel.pay_amount];
    //时间
    cell.timeLabel.text = oilOrderModel.create_time;
    
    //支付方式
    if (![oilOrderModel.status isEqualToString:@"0"]) {
        
        if ([oilOrderModel.pay_type isEqualToString:@"1"]) {
            
            NSMutableAttributedString * mStr = [[NSMutableAttributedString alloc]initWithString:@"支付类型: 充值账户 (已付款)"];
            NSDictionary * dict1 = @{
                                     NSForegroundColorAttributeName:[UIColor redColor]};
            [mStr addAttributes:dict1 range:NSMakeRange(6, 10)];
            cell.ztLabel.attributedText = mStr;
            
        }else if ([oilOrderModel.pay_type isEqualToString:@"2"]){
            
            NSMutableAttributedString * mStr = [[NSMutableAttributedString alloc]initWithString:@"支付类型: 代金券 (已付款)"];
            NSDictionary * dict1 = @{
                                     NSForegroundColorAttributeName:[UIColor redColor]};
            [mStr addAttributes:dict1 range:NSMakeRange(6, 9)];
            cell.ztLabel.attributedText = mStr;
            
        }else if ([oilOrderModel.pay_type isEqualToString:@"3"]){
            
            NSMutableAttributedString * mStr = [[NSMutableAttributedString alloc]initWithString:@"支付类型: 支付宝 (已付款)"];
            NSDictionary * dict1 = @{
                                     NSForegroundColorAttributeName:[UIColor redColor]};
            [mStr addAttributes:dict1 range:NSMakeRange(6, 9)];
            cell.ztLabel.attributedText = mStr;
            
        }else if ([oilOrderModel.pay_type isEqualToString:@"4"]){
            
            NSMutableAttributedString * mStr = [[NSMutableAttributedString alloc]initWithString:@"支付类型: 微信 (已付款)"];
            NSDictionary * dict1 = @{
                                     NSForegroundColorAttributeName:[UIColor redColor]};
            [mStr addAttributes:dict1 range:NSMakeRange(6, 8)];
            cell.ztLabel.attributedText = mStr;
            
        }else{
            
            cell.ztLabel.text = @"支付类型: 未付款";
        }
        
    }else{
        
        cell.ztLabel.text = @"支付类型: 未付款";
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 185;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSMutableArray *)oilOrderArray
{
    if (!_oilOrderArray) {
        _oilOrderArray = [NSMutableArray array];
    }
    return _oilOrderArray;
}

@end
